import DebugUtil from "../utils/DebugUtil";

/**
 * HTTP  网络工具 基于Creator
 */
export default class HttpUtils{

        /**
         * get 请求
         * @param {*} url 
         * @param {*} callback 
         */
       static httpGets(url, callback,failCb:Function = null){
            let t = Date.now();
            let xhr = cc.loader.getXMLHttpRequest();
            
            xhr.onreadystatechange = function () {
                if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) { 
                    var respone = JSON.parse(xhr.responseText);
					//var header = xhr.getResponseHeader("X-Token");
					//respone.header = header;
                    
                    DebugUtil.log(`${url}请求耗时：`,Date.now() - t);
                    callback(respone);
                }else{
                    failCb && failCb();
                }
            };
            xhr.open("GET", url, true);
            if (cc.sys.isNative) {
                xhr.setRequestHeader("Accept-Encoding","gzip,deflate");
            }
     
            // note: In Internet Explorer, the timeout property may be set only after calling the open()
            // method and before calling the send() method.
            xhr.timeout = 5000;// 5 seconds for timeout
     
            xhr.send();
        }



        /**
         * post 请求
         * @param {*} url 
         * @param {*} params 
         * @param {*} callback 
         */
        static httpPost(url, params,callback) {
            var xhr = cc.loader.getXMLHttpRequest(); 
            xhr.onreadystatechange = function () {
                //cc.log('xhr.readyState='+xhr.readyState+'  xhr.status='+xhr.status);
                if (xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)) { 
                    console.log(xhr.responseText)
                    var respone = JSON.parse(xhr.responseText);
                    callback(respone);
                }else{
                      callback(-1);
                }
            };
            
            xhr.open("POST", url,true);
            if (cc.sys.isNative) {
                xhr.setRequestHeader("Accept-Encoding","gzip,deflate"); 
            }
            // note: In Internet Explorer, the timeout property may be set only after calling the open()
            // method and before calling the send() method.
            xhr.timeout = 5000;// 5 seconds for timeout   
            xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); 
            xhr.send(params);
        }


        /**
         * 下载
         * @param {*} url 
         * @param {*} path 
         * @param {*} params 
         * @param {*} callback 
         */
        static downLoad(url, path, params, callback) {
            var xhr = cc.loader.getXMLHttpRequest();
            xhr.timeout = 5000;
            var requestURL = url + path;
            if (params) {
                requestURL = requestURL + "?" + params;
            }
    
            xhr.responseType = "arraybuffer";  
            xhr.open("GET",requestURL, true);
            if (cc.sys.isNative){
                xhr.setRequestHeader("Accept", "text/html");
                xhr.setRequestHeader("Accept-Charset", "utf-8");
                xhr.setRequestHeader("Accept-Encoding", "gzip,deflate");
            }
    
            xhr.onreadystatechange = function() {
                if(xhr.readyState === 4 && (xhr.status >= 200 && xhr.status < 300)){
                    var buffer = xhr.response;
                    var dataview = new DataView(buffer);
                    var ints = new Uint8Array(buffer.byteLength);
                    for (var i = 0; i < ints.length; i++) {
                        ints[i] = dataview.getUint8(i);
                    }
                    callback(null, ints);
                }
                else {
                    callback(xhr.readyState + ":" + xhr.status, null);
                }
            };
            xhr.send();
            return xhr;
        } 
}
